package com.quanquan.management.service;

import DTO.PageDTO;
import com.quanquan.management.core.service.GenericService;
import com.quanquan.management.dto.Children;
import com.quanquan.management.dto.PermissionDTO;
import com.quanquan.management.dto.PermissionResultDTO;
import com.quanquan.management.dto.SysPermissionDTO;
import com.quanquan.management.entity.SysPermission;
import com.quanquan.management.repository.SysPermissionRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.LinkedList;
import java.util.List;

@Service
public class SysPermissionService extends GenericService<SysPermission, String> {

    @Autowired
    private SysPermissionRepository sysPermissionRepository;

    @Autowired
    private SysPermissionRoleService sysPermissionRoleService;

    @PostConstruct
    private void init() {
        setGenericRepository(sysPermissionRepository);
    }


    /**
     * 查询所有权限
     *
     * @return
     */
    public PageDTO<SysPermissionDTO> queryAllPermission() {
        PageDTO<SysPermissionDTO> objectPageDTO = new PageDTO<>();

        Pageable pageable = PageRequest.of(0, 999);
        SysPermission sysRole = new SysPermission();

        Example<SysPermission> example = Example.of(sysRole);
        Page<SysPermission> findAll = sysPermissionRepository.findAll(example, pageable);
        List<SysPermission> all = findAll.getContent();
        List<SysPermissionDTO> sysPermissionDTOList = new LinkedList<>();
        for (SysPermission sysPermission : all) {
            SysPermissionDTO sysPermissionDTO = new SysPermissionDTO();
            //通过映射关系来赋值 //TODO 断点看一下
            BeanUtils.copyProperties(sysPermission, sysPermissionDTO);
            sysPermissionDTOList.add(sysPermissionDTO);
        }
        objectPageDTO.setTotal(findAll.getTotalElements());
        objectPageDTO.setTotalPage(0);
        objectPageDTO.setPageSize(999);
        objectPageDTO.setList(sysPermissionDTOList);
        return objectPageDTO;
    }


    public LinkedList<PermissionDTO> queryResult() {

        LinkedList<PermissionDTO> objects = new LinkedList<>();

        //查询出所以有1级的权限
        List<SysPermission> superior = sysPermissionRepository.findBySysLevel(1);
        for (SysPermission sysPermission : superior) {

            PermissionDTO permissionDTO = new PermissionDTO();
            //通过映射关系来赋值 //TODO 断点看一下
            BeanUtils.copyProperties(sysPermission, permissionDTO);
            permissionDTO.setName(sysPermission.getName());

            String id = sysPermission.getId();
            permissionDTO.setOpen(false);
            permissionDTO.setCascade(true);

            List<SysPermission> sysPermissionList = sysPermissionRepository.findByParentIdAndLevel(id, 2);
            LinkedList<Children> objects2 = new LinkedList<>();
            for (SysPermission sysPermission1 : sysPermissionList) {

                List<SysPermission> sysPermissions = sysPermissionRepository.findByParentIdAndLevel(sysPermission1.getId(), 3);
                Children children1 = new Children();
                children1.setId(sysPermission1.getId());
                children1.setName(sysPermission1.getName());
                children1.setOpen(false);
                LinkedList<Children> objects1 = new LinkedList<>();
                for (SysPermission sysPermission2 : sysPermissions) {
                    Children children = new Children();
                    children.setId(sysPermission2.getId());
                    children.setName(sysPermission2.getName());
                    children.setOpen(false);
                    objects1.add(children);
                }

                children1.setChildren(objects1);
                objects2.add(children1);
            }
            permissionDTO.setChildren(objects2);

            objects.add(permissionDTO);
        }

        return objects;
    }


    public PermissionResultDTO queryResultEdit(String roleId) {

        PermissionResultDTO permissionResultDTO = new PermissionResultDTO();


        LinkedList<PermissionDTO> objects = new LinkedList<>();
        List<String> permissionIds = sysPermissionRoleService.queryPermissionIds(roleId);

        LinkedList<PermissionDTO> permissionDTOS = queryResult();

//        if (permissionIds.size() <= 0) {
//            LinkedList<PermissionDTO> permissionDTOS = queryResult();
//            return permissionDTOS;
//        }

        permissionResultDTO.setPermissionIds(permissionIds);
        permissionResultDTO.setPermissionDTOS(permissionDTOS);
//        //查询所有权限
//        List<PermissionInfo> permissionInfos = getPermissionInfos(permissionIds);
//
//        //查询出所以有1级的权限
//        List<PermissionInfo> firstPermissions = permissionInfos.parallelStream().filter(e -> e.getLevel() == 1).collect(Collectors.toList());
//        for (PermissionInfo permissionInfo : firstPermissions) {
//            PermissionDTO permissionDTO = new PermissionDTO();
//            //通过映射关系来赋值 //TODO 断点看一下
//            BeanUtils.copyProperties(permissionInfo, permissionDTO);
//            String id = permissionInfo.getId();
//            //查询出所以有1级下的二级的权限
//            List<PermissionInfo> secendPermissions = getPermissionInfosByParentId(permissionIds,id,2);
//            LinkedList<Children> objects2 = new LinkedList<>();
//
//            for (PermissionInfo secendPermission : secendPermissions) {
//                String id1 = secendPermission.getId();
//                List<PermissionInfo> threePermissions = getPermissionInfosByParentId(permissionIds,id1,3);
//                Children children1 = new Children();
//                children1.setId(secendPermission.getId());
//                children1.setTitle(secendPermission.getTitle());
//                children1.setChecked(secendPermission.getChecked());
//
//                LinkedList<Children> objects1 = new LinkedList<>();
//                for (PermissionInfo sysPermission2 : threePermissions) {
//                    Children children = new Children();
//                    children.setId(sysPermission2.getId());
//                    children.setTitle(sysPermission2.getTitle());
//                    children.setChecked(sysPermission2.getChecked());
//                    objects1.add(children);
//                }
//                children1.setChildren(objects1);
//                objects2.add(children1);
//            }
//            permissionDTO.setChildren(objects2);
//
//            objects.add(permissionDTO);
//        }
        return permissionResultDTO;
    }

//    public List<PermissionInfo> getPermissionInfos(List<String> permissionIds) {
//        List<SysPermission> all = sysPermissionRepository.findAll();
//        List<PermissionInfo> permissionInfos = all.parallelStream().map(e -> new PermissionInfo(
//                e.getId(),
//                e.getLevel(),
//                e.getName(),
//                e.getParentId(),
//                false
//        )).collect(Collectors.toList());
//
//        for (String permissionId : permissionIds) {
//            for (PermissionInfo permissionInfo : permissionInfos) {
//                if (permissionInfo.getId().equals(permissionId)) {
//                    permissionInfo.setChecked(true);
//                }
//            }
//        }
//        return permissionInfos;
//    }
//
//    public List<PermissionInfo> getPermissionInfosByParentId(List<String> permissionIds, String id,Integer level) {
//        List<SysPermission> all = sysPermissionRepository.findByParentIdAndLevel(id,level);
//        List<PermissionInfo> permissionInfos = all.parallelStream().map(e -> new PermissionInfo(
//                e.getId(),
//                e.getLevel(),
//                e.getName(),
//                e.getParentId(),
//                false
//        )).collect(Collectors.toList());
//
//        for (String permissionId : permissionIds) {
//            for (PermissionInfo permissionInfo : permissionInfos) {
//                if (permissionInfo.getId().equals(permissionId)) {
//                    permissionInfo.setChecked(true);
//                }
//            }
//        }
//        return permissionInfos;
//    }
}
